//
//  WYCSummaryController.m
//  51还款
//
//  Created by 中企互联 on 2017/10/30.
//  Copyright © 2017年 中企互联. All rights reserved.
//

#import "WYCSummaryController.h"
#import "WYChuizongModel.h"
#import "CATCurveProgressView.h"
#import "huizongModel.h"
#import "WYChuizongCell.h"
#import "SJAxisView.h"
#import "SJChartLineView.h"
#import "SJLineChart.h"


@interface WYCSummaryController ()<CATCurveProgressViewDelegate,UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *totalView;
@property(nonatomic,strong)NSArray  *xLb;
@property (nonatomic, strong) SJLineChart *lineChart;

@property(nonatomic,strong)CATCurveProgressView *progressView1;

@property(nonatomic,strong)CATCurveProgressView *progressView2;

@property(nonatomic,strong)CATCurveProgressView *progressView3;
@property(nonatomic,strong)CALayer *bottomLineLayer;

/** 几张卡*/
@property(nonatomic,strong)UILabel * Totallabel;

/** 几月*/
@property(nonatomic,strong)UILabel * monthlabel;

/** 本期*/
@property(nonatomic,strong)UILabel * benqilable;

/** 已还*/
@property(nonatomic,strong)UILabel * yihuanlabel;

/** 未还*/
@property(nonatomic,strong)UILabel * weihuanlabel;

/** 列表*/
@property(nonatomic,strong)UITableView * tableView;

/** 汇总*/
@property(nonatomic,strong)WYChuizongModel * huizong;

/** 折线图数组*/
@property (strong , nonatomic) NSArray * arr;


/** 数组*/
@property (strong , nonatomic) NSMutableArray * huizongarr;

/** str*/
@property (strong , nonatomic) NSString *str;
@end

@implementation WYCSummaryController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupRequster];
    self.arr = [[NSArray alloc] init];
    self.huizongarr = [[NSMutableArray alloc] init];
    //界面布局
    
    [self setupTableView];
    
    
    
}


#pragma mark ------>>创建tableview
- (void)setupTableView{
    
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NavHeight, ScreenWidth, ScreenHeight - (NavHeight)) style:UITableViewStylePlain];
    self.tableView.backgroundColor = WYCGlobalBg;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    //    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    
    [self.view addSubview:self.tableView];
    
}
- (void)setupRequster{
    NSString * str = [NSString stringWithFormat:@"%@token=%@",billstatis,[WYCAccountTool unarchivetoken]];
    [HttpTool POST:str params:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        WYCLog(@"%@",responseObject);
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            NSDictionary * resultdict = responseObject[@"result"];
            NSArray * arr = resultdict[@"billDetailList"];
            for (NSDictionary * dict in arr) {
                huizongModel * hzModel = [huizongModel mj_objectWithKeyValues:dict];
                [self.huizongarr addObject:hzModel];
            }
            
            self.huizong = [WYChuizongModel mj_objectWithKeyValues:resultdict];
            
            self.arr = resultdict[@"everyMonthConsumeAmount"];
            [self setupTableViewHeader];
            
        }else{
            
            [self showToast:msgStr];
        }
        
        [self.tableView reloadData];
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
}
#pragma make ------- 表视图头部
- (void)setupTableViewHeader{
    UIView * headerView = [[UIView alloc] init];
    headerView.backgroundColor = [UIColor whiteColor];
    CGFloat headerViewX = 0;
    CGFloat headerViewY = (NavHeight);
    CGFloat headerViewW = ScreenWidth;
    CGFloat headerViewH = 2200 * px;
    
    headerView.frame = CGRectMake(headerViewX, headerViewY, headerViewW, headerViewH);
    
    
    //信用卡张数
    self.Totallabel = [[UILabel alloc] init];
    //    Totallabel.backgroundColor = [UIColor redColor];
    self.Totallabel.text = [NSString stringWithFormat:@"共有信用卡 %@ 张",self.huizong.creditCardCount];
    self.Totallabel.frame = CGRectMake(0, 40 * px, ScreenWidth, 60 * px);
    self.Totallabel.font = [UIFont systemFontOfSize:SmallFont];
    self.Totallabel.textAlignment = NSTextAlignmentCenter;
    self.Totallabel.textColor = [UIColor grayColor];
    [headerView addSubview:self.Totallabel];
    WYCLog(@"------------%@",self.Totallabel.text);
    //    self.Totallabel = Totallabel;
    
    
    //月份账单
    UILabel * monthlabel = [[UILabel alloc] init];
    monthlabel.frame = CGRectMake(0, CGRectGetMaxY(self.Totallabel.frame) + 40 * px, ScreenWidth, 60 * px);
    monthlabel.font = MiddleLight;
    monthlabel.text = [NSString stringWithFormat:@"%@年%@月账单",self.huizong.year,self.huizong.month];
    monthlabel.textAlignment = NSTextAlignmentCenter;
    monthlabel.textColor = WYZBlackBg;
    [headerView addSubview:monthlabel];
    self.monthlabel = monthlabel;
    
    
    self.progressView1 = [[CATCurveProgressView alloc] init];
    self.progressView1.frame = CGRectMake(80 * px, CGRectGetMaxY(monthlabel.frame) + 100, 150, 150 * px);
    //    self.progressView1.backgroundColor = [UIColor whiteColor];
    self.progressView1.tintColor = [UIColor whiteColor];
    self.progressView1.curveBgColor = WYCRGBColor(212, 212, 212);
    self.progressView1.progressColor = WYCMain;
    [self performSelector:@selector(changeProgress) withObject:nil afterDelay:1.0];
    [headerView addSubview:self.progressView1];
    
    
    self.benqilable = [[UILabel alloc] init];
    self.benqilable.frame = CGRectMake(self.progressView1.x, CGRectGetMaxY(self.progressView1.frame) + 30, self.progressView1.width, 70 * px);
    self.benqilable.font = [UIFont systemFontOfSize:BigFont];
    self.benqilable.textAlignment = NSTextAlignmentCenter;
    self.benqilable.textColor = [UIColor blackColor];
    if ([self.huizong.monthBillAmount isEqualToString:@".00"]) {
        self.benqilable.text = [NSString stringWithFormat:@"0%@",self.huizong.monthBillAmount];
    }else{
        self.benqilable.text = [NSString stringWithFormat:@"%@",self.huizong.monthBillAmount];
    }
    [headerView addSubview:self.benqilable];
    
    UILabel * onelabel = [[UILabel alloc] init];
    onelabel.frame = CGRectMake(self.benqilable.x, CGRectGetMaxY(self.benqilable.frame) + 20 * px, self.benqilable.width, 70 * px);
    onelabel.font = [UIFont systemFontOfSize:SmallFont];
    onelabel.textAlignment = NSTextAlignmentCenter;
    onelabel.textColor = [UIColor grayColor];
    onelabel.text = @"本期账单";
    [headerView addSubview:onelabel];
    
    
    self.progressView2 = [[CATCurveProgressView alloc] init];
    self.progressView2.frame = CGRectMake(ScreenWidth - 80 * px - 100, CGRectGetMaxY(monthlabel.frame) + 100 * px, 100, 100 * px);
    //    self.progressView1.backgroundColor = [UIColor whiteColor];
    self.progressView2.tintColor = [UIColor whiteColor];
    self.progressView2.curveBgColor = WYCRGBColor(212, 212, 212);
    self.progressView2.progressColor = [UIColor blueColor];
    [self performSelector:@selector(changeProgress) withObject:nil afterDelay:1.0];
    [headerView addSubview:self.progressView2];
    
    
    self.yihuanlabel = [[UILabel alloc] init];
    self.yihuanlabel.frame = CGRectMake(self.progressView2.x, CGRectGetMaxY(self.progressView2.frame) + 30, self.progressView2.width, 70 * px);
    self.yihuanlabel.font = [UIFont systemFontOfSize:BigFont];
    self.yihuanlabel.textAlignment = NSTextAlignmentCenter;
    self.yihuanlabel.textColor = [UIColor blackColor];
    if ([self.huizong.alReadyBillAmount isEqualToString:@".00"]) {
        self.yihuanlabel.text = [NSString stringWithFormat:@"0%@",self.huizong.alReadyBillAmount];
    }else{
        self.yihuanlabel.text = [NSString stringWithFormat:@"%@",self.huizong.alReadyBillAmount];
    }
    [headerView addSubview:self.yihuanlabel];
    
    
    UILabel * YHlabel = [[UILabel alloc] init];
    YHlabel.frame = CGRectMake(self.yihuanlabel.x, CGRectGetMaxY(self.yihuanlabel.frame) + 20 * px, self.yihuanlabel.width, 70 * px);
    YHlabel.font = [UIFont systemFontOfSize:SmallFont];
    YHlabel.textAlignment = NSTextAlignmentCenter;
    YHlabel.textColor = [UIColor grayColor];
    YHlabel.text = @"已还账单";
    [headerView addSubview:YHlabel];
    
    self.progressView3 = [[CATCurveProgressView alloc] init];
    self.progressView3.frame = CGRectMake(self.progressView2.x, CGRectGetMaxY(self.progressView1.frame) + 250 * px, 100, 100 * px);
    //    self.progressView1.backgroundColor = [UIColor whiteColor];
    self.progressView3.tintColor = [UIColor whiteColor];
    self.progressView3.curveBgColor = WYCRGBColor(212, 212, 212);
    self.progressView3.progressColor = [UIColor greenColor];
    [self performSelector:@selector(changeProgress) withObject:nil afterDelay:1.0];
    [headerView addSubview:self.progressView3];
    
    
    
    self.weihuanlabel = [[UILabel alloc] init];
    self.weihuanlabel.frame = CGRectMake(self.progressView3.x, CGRectGetMaxY(self.progressView3.frame) + 30, self.progressView3.width, 70 * px);
    self.weihuanlabel.font = [UIFont systemFontOfSize:BigFont];
    self.weihuanlabel.textAlignment = NSTextAlignmentCenter;
    self.weihuanlabel.textColor = [UIColor blackColor];
    if ([self.huizong.unAlreadyBillAmount isEqualToString:@".00"]) {
        self.weihuanlabel.text = [NSString stringWithFormat:@"0%@",self.huizong.unAlreadyBillAmount];
    }else{
        self.weihuanlabel.text = [NSString stringWithFormat:@"%@",self.huizong.unAlreadyBillAmount];
    }
    [headerView addSubview:self.weihuanlabel];
    
    
    UILabel * WHlabel = [[UILabel alloc] init];
    WHlabel.frame = CGRectMake(self.weihuanlabel.x, CGRectGetMaxY(self.weihuanlabel.frame) + 20 * px, self.weihuanlabel.width, 70 * px);
    WHlabel.font = [UIFont systemFontOfSize:SmallFont];
    WHlabel.textAlignment = NSTextAlignmentCenter;
    WHlabel.textColor = [UIColor grayColor];
    WHlabel.text = @"未还账单";
    [headerView addSubview:WHlabel];
    
    
    UIView *  backView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(WHlabel.frame) + 40 * px, ScreenWidth, 30 * px)];
    backView.userInteractionEnabled = YES;
    backView.backgroundColor = [UIColor whiteColor];
    [headerView addSubview:backView];
    
    
    
    
    UIScrollView  *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(backView.frame),ScreenWidth , 200)];
    scrollView.backgroundColor = [UIColor whiteColor];
    scrollView.contentSize = CGSizeMake(ScreenWidth*2, 200);  //非常重要
    scrollView.userInteractionEnabled = YES;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    [headerView addSubview:scrollView];
    
    //横坐标数据数组
    NSString * str1 = self.arr[0];
    NSString * str2 = self.arr[1];
    NSString * str3 = self.arr[2];
    NSString * str4 = self.arr[3];
    NSString * str5 = self.arr[4];
    NSString * str6 = self.arr[5];
    NSString * str7 = self.arr[6];
    NSString * str8 = self.arr[7];
    NSString * str9 = self.arr[8];
    NSString * str10 = self.arr[9];
    NSString * str11 = self.arr[10];
    NSString * str12 = self.arr[11];
    //横坐标数据数组
    _xLb = @[str1,str2,str3,str4,str5,str6,str7,str8,str9,str10,str11,str12];
    
    // 初始化折线图
    self.lineChart = [[SJLineChart alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth*2-40, 200)];
    
    // 设置折线图属性
    
    self.lineChart.title = @"消费趋势图"; // 折线图名称
    self.lineChart.maxValue = 6;   // 最大值
    
    NSMutableArray *tempArr = [NSMutableArray array];
    
    NSArray *tempMonthArr = @[@"一",@"二",@"三",@"四",@"五",@"六",@"七",@"八",@"九",@"十",@"十一",@"十二"];
    
    for (int i = 0; i<self.xLb.count; i++)
    {
        NSInteger str = [self.xLb[i] integerValue];
        NSDictionary *tempDic=@{@"item":[NSString stringWithFormat:@"%@月",tempMonthArr[i]],@"count":@(str)};
        [tempArr addObject:tempDic];
    }
    
    int maxt = [[self.xLb valueForKeyPath:@"@max.intValue"] intValue];
    
    NSMutableArray *tempYTit=[NSMutableArray array];
    for (int i=0; i<5;i++)
    {
        NSString *str = [NSString stringWithFormat:@"%d",i*maxt/6];
        [tempYTit addObject:str];
    }
    [tempYTit addObject:[NSString stringWithFormat:@"%d",maxt]];
    
    self.lineChart.maxValue =  maxt;
    self.lineChart.yMarkTitles = tempYTit;
    
    [self.lineChart setXMarkTitlesAndValues:tempArr titleKey:@"item" valueKey:@"count"];
    [self.lineChart mapping];
    
    [scrollView addSubview:self.lineChart];
    
    
    UIView *  billView = [[UIView alloc]initWithFrame:CGRectMake(0, scrollView.frame.size.height+scrollView.frame.origin.y+10, ScreenWidth, 40)];
    billView.userInteractionEnabled = YES;
    billView.backgroundColor = [UIColor whiteColor];
    [headerView addSubview:billView];
    UILabel *  billLb= [[UILabel alloc] init];
    billLb.frame = CGRectMake(0,10, ScreenWidth, 68 * px);
    billLb.textAlignment = NSTextAlignmentCenter;
    billLb.textColor = [UIColor blackColor];
    if (self.huizongarr.count == 0) {
        billLb.text = @"";
    }else{
        billLb.text = @"账单明细";
    }
    billLb.font = [UIFont systemFontOfSize:BigFont];
    [billView addSubview:billLb];
    UIView  *line = [[UIView alloc]initWithFrame:CGRectMake(0, 39, ScreenWidth, 1)];
    line.backgroundColor = [UIColor grayColor];
    //    [billView  addSubview:line];
    self.tableView.tableHeaderView = headerView;
    
}


-(void)changeProgress{
    self.progressView1.progress = 1.0f;
    _progressView2.progress = 1.0f;
    _progressView3.progress = 1.0f;
    //    _progressView4.progress = 1.0f;
}
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = YES;
    
    
    UIView * NavView = [[UIView alloc] init];
    NavView.frame = CGRectMake(0, 0, ScreenWidth, NavHeight);
    NavView.backgroundColor = WYCMain;
    [self.view addSubview:NavView];
    
    
    
    self.bottomLineLayer = [[CALayer alloc] init];
    self.bottomLineLayer.frame = CGRectMake(0, NavView.bounds.size.height - 0.5, NavView.bounds.size.width, 0.5);
    self.bottomLineLayer.backgroundColor = WYCRGBColor(191, 191, 196).CGColor;
    [NavView.layer addSublayer:self.bottomLineLayer];
    
    
    UILabel * NavRigsterLabel = [[UILabel alloc] init];
    NavRigsterLabel.frame = CGRectMake(0, titleY, NavView.width, 68 * px);
    NavRigsterLabel.textColor = [UIColor whiteColor];
    NavRigsterLabel.font = [UIFont boldSystemFontOfSize:BigFont];
    NavRigsterLabel.textAlignment = NSTextAlignmentCenter;
    NavRigsterLabel.text = @"汇总";
    [NavView addSubview:NavRigsterLabel];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    return self.huizongarr.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return 160 * px;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    WYChuizongCell * cell = [WYChuizongCell cellWithTableView:tableView];
    
    huizongModel * model = self.huizongarr[indexPath.row];
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    cell.planAmountLabel.text = model.bankName;
    cell.timeLabel.text = model.executeTime;
    cell.proceduresLabel.text = [NSString stringWithFormat:@"%@元",model.amount];
    //消费
    WYCLog(@"------------------%@",model.type);
    NSString * typestr = model.type;
    if ([typestr isEqualToString:@"1"]) {
        cell.typeImg.image = [UIImage imageNamed:@"消费icon"];
        cell.proceduresLabel.text =  [NSString stringWithFormat:@"￥%.2f元",  [model.amount doubleValue ]+[model.poundage doubleValue]];
    }
    else
    {
        cell.typeImg.image = [UIImage imageNamed:@"还款icon"];
        cell.proceduresLabel.text =  [NSString stringWithFormat:@"￥%.2f元",  [model.amount doubleValue ]-[model.poundage doubleValue]];
    }
    
    return cell;
}



/*********************折线图****************/
//- (NSArray *)lines:(BOOL)fill {
//    WYCLog(@"--------------%@",self.arr);
//    NSString * str1 = self.arr[0];
//    NSString * str2 = self.arr[1];
//    NSString * str3 = self.arr[2];
//    NSString * str4 = self.arr[3];
//    NSString * str5 = self.arr[4];
//    NSString * str6 = self.arr[5];
//    NSString * str7 = self.arr[6];
//    NSString * str8 = self.arr[7];
//    NSString * str9 = self.arr[8];
//    NSString * str10 = self.arr[9];
//    NSString * str11 = self.arr[10];
//    NSString * str12 = self.arr[11];
//
//
//
//    NSArray *pointsArr = @[@{@"xValue" : @"一月", @"yValue" : str1},
//                           @{@"xValue" : @"二月", @"yValue" : str2},
//                           @{@"xValue" : @"三月", @"yValue" : str3},
//                           @{@"xValue" : @"四月", @"yValue" : str4},
//                           @{@"xValue" : @"五月", @"yValue" : str5},
//                           @{@"xValue" : @"六月", @"yValue" : str6},
//                           @{@"xValue" : @"七月", @"yValue" : str7},
//                           @{@"xValue" : @"八月", @"yValue" : str8},
//                           @{@"xValue" : @"九月", @"yValue" : str9},
//                           @{@"xValue" : @"十月", @"yValue" : str10},
//                           @{@"xValue" : @"十一月", @"yValue" : str11},
//                           @{@"xValue" : @"十二月", @"yValue" : str12}];
//
//
//    NSMutableArray *points = @[].mutableCopy;
//    for (int i = 0; i < pointsArr.count; i++) {
//        PointItem *item = [[PointItem alloc] init];
//        NSDictionary *itemDic = pointsArr[i];
//        item.price = itemDic[@"yValue"];
//        item.time = itemDic[@"xValue"];
//        item.chartLineColor = WYCMain;
//        item.chartPointColor = WYCMain;
//        item.pointValueColor = WYCMain;
//        if (fill) {
//            item.chartFillColor = [UIColor colorWithRed:0 green:0.5 blue:0.2 alpha:0.5];
//            item.chartFill = YES;
//        }
//        [points addObject:item];
//    }
//
//    return @[points];
//}
//
//#pragma mark PXLineChartViewDelegate
////通用设置
//- (NSDictionary<NSString*, NSString*> *)lineChartViewAxisAttributes {
//    return @{yElementInterval : @"40",
//             xElementInterval : @"40",
//             yMargin : @"50",
//             xMargin : @"25",
//             yAxisColor : [UIColor colorWithRed:200.0/255 green:200.0/255 blue:200.0/255 alpha:1],
//             xAxisColor : [UIColor colorWithRed:200.0/255 green:200.0/255 blue:200.0/255 alpha:1],
//             gridColor : [UIColor colorWithRed:244.0/255 green:244.0/255 blue:244.0/255 alpha:1],
//             gridHide : @0,
//             pointHide : @0,
//             pointFont : [UIFont systemFontOfSize:10],
//             firstYAsOrigin : @1,
//             scrollAnimation : @1,
//             scrollAnimationDuration : @"2"};
//}
////line count
//- (NSUInteger)numberOfChartlines {
//    return self.lines.count;
//}
////x轴y轴对应的元素count
//- (NSUInteger)numberOfElementsCountWithAxisType:(AxisType)axisType {
//    return (axisType == AxisTypeY)? _yElements.count : _xElements.count;
//}
////x轴y轴对应的元素view
//- (UILabel *)elementWithAxisType:(AxisType)axisType index:(NSUInteger)index {
//    UILabel *label = [[UILabel alloc] init];
//    NSString *axisValue = @"";
//    if (axisType == AxisTypeX) {
//        axisValue = _xElements[index];
//        label.textAlignment = NSTextAlignmentCenter;//;
//    }else if(axisType == AxisTypeY){
//        axisValue = _yElements[index];
//        label.textAlignment = NSTextAlignmentRight;//;
//    }
//    label.text = axisValue;
//    label.font = [UIFont systemFontOfSize:12];
//    label.textColor = [UIColor blackColor];
//    return label;
//}
////每条line对应的point数组
//- (NSArray<id<PointItemProtocol>> *)plotsOflineIndex:(NSUInteger)lineIndex {
//    return self.lines[lineIndex];
//}
////点击point回调响应
//- (void)elementDidClickedWithPointSuperIndex:(NSUInteger)superidnex pointSubIndex:(NSUInteger)subindex {
//
//    WYCLogFunc;
//    PointItem *item = self.lines[superidnex][subindex];
//    NSString *xTitle = item.time;
//    NSString *yTitle = item.price;
//    UIAlertController *alertView = [UIAlertController alertControllerWithTitle:yTitle
//                                                                       message:nil preferredStyle:UIAlertControllerStyleAlert];
//    [alertView addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//
//    }]];
//    [self presentViewController:alertView animated:YES completion:nil];
//}
//
//static bool fill = NO;
//- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    fill = !fill;
//    self.lines = [self lines:fill];
//    [_pXLineChartView reloadData];
//}
//
//- (void)didReceiveMemoryWarning {
//    [super didReceiveMemoryWarning];
//    // Dispose of any resources that can be recreated.
//}



@end


